<?php
namespace app\common\library;

use think\Exception;

class Pay {
    /**
     * @var object 操作句柄
     */
    protected static $handler;
    
    /**
     * @var array Token的实例
     */
    protected static $instance = [];
    
    /**
     * 初始化
     * @param string $type
     * @throws Exception
     */
    private static function init($type){
        $type = strtolower($type);
        
        $class = '\\app\\common\\library\\payment\\type\\' . ucwords($type);
        if (class_exists($class)) {
            if(!isset(self::$instance[$type])){
                self::$instance[$type] = new $class();
            }
        } else {
            throw new Exception('支付类没有找到');
        }
        
        return self::$instance[$type];
    }
    
    /**
     * 统一下单
     * @param string $type
     * @param string $title
     * @param string $out_trade_no
     * @param float $total
     */
    public static function unifiedorder($type, $title, $out_trade_no, $total){
        return self::init($type)->unifiedorder($title, $out_trade_no, $total);
    }
    
    /**
     * 检测回调
     * @param string $type
     * @param array $data
     * @return boolean
     */
    public static function check($type, $data){
        return self::init($type)->check($data);
    }
}